package de.reichelt.fx;

import java.text.SimpleDateFormat;
import java.util.logging.Logger;
import javafx.scene.control.DatePicker;

public class FxDateValidator extends FxValidator {
  
  private Logger log;
  private SimpleDateFormat fmt = new SimpleDateFormat("dd.MM.yyyy");

  public FxDateValidator(FxFormField field) {
    super(field);
    if (log==null) {
      log = Logger.getLogger(this.getClass().getName());
    }
  }

  @Override
  public boolean isValid() throws Exception {
    log.fine("field.value="+((DatePicker)field.getControl()).getValue());
    String txt = ((DatePicker)field.getControl()).getEditor().getText();
    if (txt==null || txt.isEmpty()) {
      return true;
    }
    try {
      fmt.parse(txt);
    } catch (Exception ex) {
      return false;
    }
    return true;
  }

  @Override
  public String getMessage() {
    return "Kein gültiges Datum.";
  }
  
}
